unit Nicknames;

interface

uses
  Database,
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  DB, ComCtrls, StdCtrls, Buttons, ExtCtrls, Grids, Menus, DBTables, DBGrids;

type
  TFormNicknames = class(TForm)
    Label1: TLabel;
    EditNickname: TEdit;
    BtnAdd: TBitBtn;
    BtnDelete: TBitBtn;
    BtnDone: TBitBtn;
    BtnHelp: TBitBtn;
    EditReplacement: TEdit;
    Label2: TLabel;
    MainMenu1: TMainMenu;
    MenuHelp: TMenuItem;
    Bevel2: TBevel;
    Bevel3: TBevel;
    Bevel4: TBevel;
    Panel1: TPanel;
    Status: TLabel;
    DSNicknames: TDataSource;
    GridNicknames: TDBGrid;
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure EditNicknameKeyPress(Sender: TObject; var Key: Char);
    procedure EditReplacementKeyPress(Sender: TObject; var Key: Char);
    procedure BtnDoneClick(Sender: TObject);
    procedure BtnDeleteClick(Sender: TObject);
    procedure BtnAddClick(Sender: TObject);
    procedure BtnHelpClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure GridNicknamesDblClick(Sender: TObject);
    procedure GridNicknamesKeyPress(Sender: TObject; var Key: Char);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormNicknames: TFormNicknames;

implementation
uses
  DBClient;
{$R *.DFM}

procedure TFormNicknames.FormMouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
begin
  Status.caption:=' '+(sender as TControl).hint;
end;

procedure TFormNicknames.EditNicknameKeyPress(Sender: TObject;
  var Key: Char);
begin
  if key = #13 then begin
    if EditNickname.text <> '' then EditReplacement.setfocus;
    key:=#0;
  end;
end;

procedure TFormNicknames.EditReplacementKeyPress(Sender: TObject;
  var Key: Char);
begin
  if key = #13 then begin
    BtnAdd.click;
    key:=#0;
  end;
end;

procedure TFormNicknames.BtnDoneClick(Sender: TObject);
begin
  close;
end;

procedure TFormNicknames.BtnDeleteClick(Sender: TObject);
begin
  if not Datawell.TableNicknames.EOF then Datawell.TableNicknames.delete;
end;

procedure TFormNicknames.BtnAddClick(Sender: TObject);
var
  i: integer;
  didfind: boolean;
  table: TClientDataset;
begin
  didfind:=false;
  if pos('"',EditNickname.text) > 0 then begin
    messagedlg('Nicknames cannot contain quotes',mtinformation,[mbok],0);
    EditNickname.setfocus;
    exit;
  end;
  i:=0;
  table:=TClientDataset.create(self);
  with table do begin
    cloneCursor(Datawell.TableNicknames, false);
    filtered := true;
    active:=true;
    first;
    while not EOF do begin
      inc(i);
      next;
    end;
  end;
  table.free;
  if i >= 1000 then begin
    messagedlg('You can only have 1000 maximum nicknames',mtinformation,[mbok],0);
    BtnDelete.setfocus;
    exit;
  end;
  if EditNickname.text = '' then begin
    messagedlg('You must enter a nickname',mtinformation,[mbok],0);
    EditNickname.setfocus;
    exit;
  end;
  if EditReplacement.text = '' then begin
    messagedlg('You must enter a replacement',mtinformation,[mbok],0);
    EditReplacement.setfocus;
    exit;
  end;
  if EditNickname.text = EditReplacement.text then begin
    messagedlg('The nickname and replacement must be different',mtinformation,[mbok],0);
    EditReplacement.selectall;
    EditReplacement.setfocus;
    exit;
  end;
  EditNickname.text:=Datawell.CutTrailingSpaces(EditNickname.text);
  table:=TClientDataset.create(self);
  with table do begin
    cloneCursor(Datawell.TableNicknames, false);
    filtered := true;
    active:=true;
    first;
    while not EOF do begin
      if fieldbyname('Nickname').asstring = EditNickname.text then begin
        didfind:=true;
        break;
      end;
      next;
    end;
  end;
  if didfind then begin
    if Datawell.TableGlobals.fieldbyname('ShowWarnings').asboolean then begin
      if messagedlg('Nickname already exists.  Replace?',
        mtconfirmation,[mbyes,mbno],0) = mrno then begin
        EditNickname.selectall;
        EditNickname.setfocus;
        table.free;
        exit;
      end;
    end;
    table.edit;
    table.fieldbyname('Replacement').asstring:=EditReplacement.text;
    table.post;
  end
  else begin
    Datawell.TableNicknames.append;
    Datawell.TableNicknames.fieldbyname('Master').asinteger:=Datawell.masterinteger;
    Datawell.TableNicknames.fieldbyname('Nickname').asstring:=EditNickname.text;
    Datawell.TableNicknames.fieldbyname('Replacement').asstring:=EditReplacement.text;
    Datawell.TableNicknames.post;
  end;
  table.free;
  EditNickname.text:='';
  EditReplacement.text:='';
  EditNickname.setfocus;
end;

procedure TFormNicknames.BtnHelpClick(Sender: TObject);
begin
  Datawell.Help('nicknames');
end;

procedure TFormNicknames.FormShow(Sender: TObject);
begin
  EditNickname.text:='';
  EditReplacement.text:='';
end;

procedure TFormNicknames.GridNicknamesDblClick(Sender: TObject);
begin
  if Datawell.TableNicknames.EOF then exit;
  EditNickname.text:=Datawell.TableNicknames.fieldbyname('Nickname').asstring;
  EditReplacement.text:=Datawell.TableNicknames.fieldbyname('Replacement').asstring;
end;

procedure TFormNicknames.GridNicknamesKeyPress(Sender: TObject;
  var Key: Char);
begin
  if key = #13 then begin
    key:=#0;
    GridNicknamesDblClick(sender);
  end;
end;

procedure TFormNicknames.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (key = VK_DELETE) and (activecontrol = GridNicknames) then begin
    BtnDelete.click;
    key:=0;
  end;
end;

end.
